from page.basepage import BasePage
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

class MainPage(BasePage):
    # 类属性，需要用到的页面元素的定位的方式
    i_close_register_dialog = (By.XPATH, "//i[text()='close']")
    i_translate_language = (By.XPATH, "//i[text()='translate']")
    div_language_type = (By.XPATH, "//div[text()='%s']")
    span_login = (By.XPATH, "//span[text()='登入']")
    div_admin_login = (By.XPATH, "//div[contains(text(), '管理员登入')]")
    inputs_name_and_password = (By.TAG_NAME, "input")
    login_button = (By.XPATH, "//div[@class='q-card']/div[3]/button")

    div_notification = (By.XPATH, "//div[contains(@class,  'q-notification__message')]")

    # 对页面元素进行的操作
    def close_register_dialog(self):
        self.find_element(*self.i_close_register_dialog).click()

    def translate_language_by_name(self, name):
        self.find_element(*self.i_translate_language).click()
        temp = list(self.div_language_type)
        temp[1] = temp[1] % name
        self.find_element(*temp).click()

    def click_login_span(self):
        self.find_element(*self.span_login).click()

    def click_div_admin_login(self):
        self.find_element(*self.div_admin_login).click()

    def input_admin_name(self, name):
        inputs = self.find_elements(*self.inputs_name_and_password)
        inputs[0].send_keys(name)

    def input_admin_password(self, password):
        inputs = self.find_elements(*self.inputs_name_and_password)
        inputs[1].send_keys(password)

    def click_admin_login_button(self):
        self.find_element(*self.login_button).click()


    # 常用的功能
    def admin_login(self, name, password):
        self.click_login_span()
        self.click_div_admin_login()
        self.input_admin_name(name)
        self.input_admin_password(password)
        self.click_admin_login_button()

    def get_notification_message(self):
        def wait_text_is_not_null(locator):
            def _method(d):
                return d.find_element(*locator).text
            return _method
        waiter = WebDriverWait(self.driver, self.timeout)
        return waiter.until(wait_text_is_not_null(self.div_notification))

